﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace EasyLibrary.AR.Classes
{
    public class Rota : ActiveRecord
    {
        public int IDRota { get; set; }
        public string Nome { get; set; }
        public DateTime DataInsercao { get; set; }
        public bool Activo { get; set; }
        public int Duracao { get; set; }
        public double Distancia { get; set; }
        public bool AutoEstradas { get; set; }
        public int Origem { get; set; }
        public bool Destino { get; set; }

        public Rota()
        {
        }

        public Rota(int id, string name, DateTime date, bool active, double dist, bool freeway, int from, bool destiny)
        {
            this.IDRota = id;
            this.Nome = name;
            this.DataInsercao = date;
            this.Activo = active;
            this.Distancia = dist;
            this.AutoEstradas = freeway;
            this.Origem = from;
            this.Destino = destiny;
        }

        protected Rota(DataRow row)
        {
            this.IDRota = Convert.ToInt32(row["IDRota"]);
            this.Nome = (string) row["Nome"];
            this.DataInsercao = (DateTime) row["DataInsercao"];
            this.Duracao = (int) row["Duracao"];
            this.Distancia = (double) row["Distancia"];
            this.AutoEstradas = (bool) row["AutoEstradas"];
            this.Origem = (int) row["Origem"];
            this.Destino = (bool) row["Destino"];
        }

        public IList LoadAll()
        {
            try
            {
                DataSet ds = ExecuteQuery(GetConnection(false),
                                          "SELECT IDRota, Nome, DataInsercao, Activo, Duracao, Distancia, AutoEstradas, Origem, Destino FROM Rotas WHERE (Activo = 'true')");

                IList ret = new ArrayList();

                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    Rota route = new Rota(r);
                    ret.Add(route);
                }

                return ret;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Erro BD", ex);
            }
        }


        public override void Save()
        {
            throw new NotImplementedException();
        }
    }
}